package com.bdmc.app.controller;

import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.work.domain.WorkOrder;
import com.bdmc.work.domain.bo.WorkOrderBo;
import com.bdmc.work.domain.vo.WorkOrderVo;
import com.bdmc.work.service.IWorkOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("app/work")
public class WorkOrderController extends BaseController {

    @Autowired
    IWorkOrderService workOrderService;

    @GetMapping("/list")
    public TableDataInfo<WorkOrderVo> list(@Validated(QueryGroup.class) WorkOrderBo bo, PageQuery pageQuery) {
        return workOrderService.mySelectPage(bo, pageQuery);
    }

    /**
     * 司机处理请求
     * @param bo bo
     * @return result
     */
    @PutMapping("driver/deal")
    public AjaxResult<Void> driverDeal(@RequestBody WorkOrderBo bo){
        WorkOrder workOrder = workOrderService.getById(bo.getOrderId());
        workOrder.setDriverDealStatus(bo.getDriverDealStatus());
        workOrder.setDriverDealTime(LocalDateTime.now());
        workOrder.setOrderStatus(2);
        return  toAjax(workOrderService.updateById(workOrder));
    }

    /**
     * 环卫人员处理请求
     * @param bo bo
     * @return result
     */
    @PutMapping("cleaner/deal")
    public AjaxResult<Void> cleanerDeal(@RequestBody WorkOrderBo bo){
        WorkOrder workOrder = workOrderService.getById(bo.getOrderId());
        workOrder.setCleanerDealStatus(bo.getCleanerDealStatus());
        if(bo.getCleanerDealStatus()==2){//已完成清运
            workOrder.setOrderStatus(3);//已完成
        }else {
            workOrder.setOrderStatus(4);//已完成,未清运
        }
        workOrder.setCleanerDealTime(LocalDateTime.now());
        workOrder.setCleanerDealContent(bo.getCleanerDealContent());
        workOrder.setCleanerDealPic(bo.getCleanerDealPic());
        return  toAjax(workOrderService.updateById(workOrder));
    }
}
